﻿using UnityEngine;
using System.Collections;

public class CameraMove : MonoBehaviour {
	

	public int Boundary = 50;
	public int speed = 5;
	public Camera kamera;

	public float cameraMaxX;
	public float cameraMinX;
	public float cameraMaxY;
	public float cameraMinY;

	private int screenWidth;
	private int screenHeight;
	

	void Start()
	{
		screenWidth = Screen.width;
		screenHeight = Screen.height;

		//docelowy aspekt kamery
		float targetaspect = 16.0f / 9.0f;
		
		// wyznaczenie aspektu okna
		float windowaspect = (float)Screen.width / (float)Screen.height;
		
		// zeskalowanie wysokości viewporta
		float scaleheight = windowaspect / targetaspect;
		
		// jeśli zeskalowana wysokość jest mniejsza od obecnej - okno w postaci 'letterbox'
		if (scaleheight < 1.0f)
		{  
			Rect rect = kamera.rect;
			
			rect.width = 1.0f;
			rect.height = scaleheight;
			rect.x = 0;
			rect.y = (1.0f - scaleheight) / 2.0f;
			
			kamera.rect = rect;
		}
		else //okno w postaci 'pillarbox'
		{
			float scalewidth = 1.0f / scaleheight;
			
			Rect rect = kamera.rect;
			
			rect.width = scalewidth;
			rect.height = 1.0f;
			rect.x = (1.0f - scalewidth) / 2.0f;
			rect.y = 0;
			
			kamera.rect = rect;
		}
	}
	void Update()
	{
		if (Input.mousePosition.x > screenWidth - Boundary && transform.position.x < cameraMaxX)
		{
			transform.position = new Vector3(
				transform.position.x + speed * Time.deltaTime,
				transform.position.y , 
				transform.position.z);
		}
		if (Input.mousePosition.x < Boundary && transform.position.x > cameraMinX)
		{
			transform.position = new Vector3(
				transform.position.x - speed * Time.deltaTime,
				transform.position.y , 
				transform.position.z);
		}
		if (Input.mousePosition.y > screenHeight - Boundary && transform.position.y < cameraMaxY)
		{
			transform.position = new Vector3(
				transform.position.x ,
				transform.position.y + speed * Time.deltaTime, 
				transform.position.z);
		}
		if (Input.mousePosition.y < Boundary && transform.position.y > cameraMinY)
		{
			transform.position = new Vector3(
				transform.position.x ,
				transform.position.y - speed * Time.deltaTime, 
				transform.position.z);
		}
	} 
}
